#include <Ogre.h>
#include <OgreApplicationContext.h>
#include <OgreSceneManager.h>
#include <OgreSceneNode.h>

class MyTestApp : public OgreBites::ApplicationContext, public OgreBites::InputListener
{
public:
    MyTestApp();
    void setup();
    bool keyPressed(const OgreBites::KeyboardEvent& evt);
};

//! [constructor]
MyTestApp::MyTestApp() : OgreBites::ApplicationContext("OgreTutorialApp")
{
}
//! [constructor]

//! [key_handler]
bool MyTestApp::keyPressed(const OgreBites::KeyboardEvent& evt)
{
    if (evt.keysym.sym == SDLK_ESCAPE)
    {
        getRoot()->queueEndRendering();
    }
    return true;
}
//! [key_handler]

//! [setup]
void MyTestApp::setup(void)
{
    // do not forget to call the base first
    OgreBites::ApplicationContext::setup();
    
    // register for input events
    addInputListener(this);

    // get a pointer to the already created root
    Ogre::Root* root = getRoot();
    Ogre::SceneManager* scnMgr = root->createSceneManager(Ogre::ST_GENERIC);

    // register our scene with the RTSS
    Ogre::RTShader::ShaderGenerator* shadergen = Ogre::RTShader::ShaderGenerator::getSingletonPtr();
    shadergen->addSceneManager(scnMgr);

    // without light we would just get a black screen    
    Ogre::Light* light = scnMgr->createLight("MainLight");
    Ogre::SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
    lightNode->setPosition(0, 10, 15);
    lightNode->attachObject(light);

    Ogre::SceneNode* pGridNode = scnMgr->getRootSceneNode()->createChildSceneNode("gridNode");
    //创建ManualObject
    Ogre::ManualObject* grid = scnMgr->createManualObject("gird");
    //使用ogre的自带材质
    grid->begin("BaseWhite",Ogre::RenderOperation::OT_TRIANGLE_LIST);
    //顶点顺序默认为逆时针可见
    grid->position(100.0f,0,0);                 //vertex1
    grid->colour(Ogre::ColourValue(1,1,1,1));
    grid->position(0,173,0);                    //vertex2
    grid->colour(Ogre::ColourValue(1,1,1,1));
    grid->position(-100.0f,0,0);                //vertex3
    grid->colour(Ogre::ColourValue(1,1,1,1));
    grid->end();
    pGridNode->attachObject(grid);

    // also need to tell where we are
    Ogre::SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
    camNode->setPosition(0, 0, 15);
    camNode->lookAt(Ogre::Vector3(0, 0, -1), Ogre::Node::TS_PARENT);

    // create the camera
    Ogre::Camera* cam = scnMgr->createCamera("myCam");
    cam->setNearClipDistance(5); // specific to this sample
    cam->setAutoAspectRatio(true);
    camNode->attachObject(cam);

    // and tell it to render into the main window
    getRenderWindow()->addViewport(cam);

    // finally something to render
    // Ogre::Entity* ent = scnMgr->createEntity("Sinbad.mesh");
    // Ogre::SceneNode* node = scnMgr->getRootSceneNode()->createChildSceneNode();
    // node->attachObject(ent);
}
//! [setup]

//! [main]
int main(int argc, char *argv[])
{
    MyTestApp app;
    app.initApp();
    app.getRoot()->startRendering();
    app.closeApp();
    return 0;
}
//! [main]
